home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Snippets / Scan Folder 1.3 / File Scanner Source / File Scanner.c++ next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  4.0 KB  |  134 lines  |  [TEXT/KAHL]

  1. #include "Scan.h"
  2.  
  3. WindowPtr bkgndWind = nil;
  4. void SetupApp();
  5.  
  6. void main() {
  7.     SetupApp();
  8.  
  9.     // --------------------------------------------------------------------------
  10.     // The following code is basically all you have to do to use
  11.     // the Scan routines:
  12.  
  13.     // Allocate 10 FSSpec records
  14.     FSSpec *fileArray = new FSSpec[10];
  15.     
  16.     short defaultVRefNum;
  17.     long defaultDirID;
  18.     long dummy;
  19.     short numFiles;
  20.     
  21.     // Get the directory of the folder that we're located in; this
  22.     // is where we'll start scanning from...
  23.     HGetVol(nil, &defaultVRefNum, &defaultDirID);
  24.  
  25.     // Alright! Let's look for our plug-in files:
  26.      numFiles = ScanFolderSpecific("\pPlug-Ins", defaultDirID, 'TEXT', 'KAHL', fileArray, 10);
  27.  
  28.     // Or you can...
  29.     // numFiles = ScanFolderByName("\pPlug-Ins", defaultDirID, "\pTeachtext dummy file", fileArray, 10);
  30.     // numFiles = ScanFolderByType("\pPlug-Ins", defaultDirID, 'sfil', fileArray, 10);
  31.     // numFiles = ScanFolderByCreator("\pPlug-Ins", defaultDirID, 'ttxt', fileArray, 10);
  32.     // numFiles = ScanFolder("\pPlug-Ins", defaultDirID, fileArray, 10);
  33.     // Or you can start searching in app's folder, not in Plug-Ins folder:
  34.     // numFiles = ScanFolderByType(nil, defaultDirID, 'TEXT', fileArray, 10);
  35.  
  36.     // Cool!! All of our plug-in files (if we had any) have been placed
  37.     // in the FSSpec array, fileArray. All we have to do now is loop through them
  38.     // and do our thing (in this example, just opening them and closing them).
  39.     
  40.     // --------------------------------------------------------------------------
  41.     // The following is application-specific stuff
  42.  
  43.     // Do some "quick 'n dirty" quickdrawing...
  44.     Str255 tempStr;
  45.     NumToString(numFiles, tempStr);
  46.     MoveTo(20, 20);
  47.     DrawString("\pNum of files found of type 'TEXT' by 'KAHL' in Plug-Ins folder: ");
  48.     DrawString(tempStr);
  49.     MoveTo(20, 40); DrawString("\pClick to continue...");
  50.     
  51.     while (!Button()) {} Delay(10, &dummy); FlushEvents(everyEvent, 0);
  52.     ForeColor(redColor);
  53.     MoveTo(20, 70); DrawString("\pName of files in Plug-Ins folder:");
  54.     // Not necessary:
  55.     //PrefixColons(fileArray, numFiles);
  56.     for (short i = 0; i < numFiles; i++) {
  57.         MoveTo(40, 90 + (i * 20));
  58.         DrawString(fileArray[i].name);
  59.     }
  60.     
  61.     // ------------------------------------------------------------------------
  62.     // Example of how to use FSSpec's to open data forks (text) or open
  63.     // resource files—it's pretty simple once you get the hang of it...
  64.     
  65.     short fsErr;
  66.     short fileRefNum;
  67.     Boolean allOkay = true;
  68.     
  69.     ForeColor(greenColor);
  70.     MoveTo(20, bkgndWind->portRect.bottom - 10);
  71.     DrawString("\pOpening file # ");
  72.     while (!Button()) {} Delay(10, &dummy); FlushEvents(everyEvent, 0);
  73.     for (i = 0; i < numFiles; i++) {
  74.         // FSpOpenDF has no problems open files with or without
  75.         // data forks OR resource forks.
  76.         fsErr = FSpOpenDF(&fileArray[i], fsRdPerm, &fileRefNum);
  77.         if (fsErr != noErr) {
  78.             allOkay = false;
  79.             break;
  80.         }
  81.         FSClose(dummy);
  82.         
  83.         // FSpOpenResFile will return an error if the file
  84.         // doesn't have a resource fork... Since there's no
  85.         // resource fork, error -39 will be returned, end
  86.         // of file [EOF] error... (text files created
  87.         // by Think Project Manager have resources; text files
  88.         // by Teachtext don't)
  89.         fileRefNum = FSpOpenResFile(&fileArray[i], fsRdPerm);
  90.         if (fileRefNum == -1) {
  91.             allOkay = false;
  92.             fsErr = ResError();
  93.             break;
  94.         }
  95.         CloseResFile(fileRefNum);
  96.  
  97.         NumToString(i +1, tempStr);
  98.         DrawString(tempStr);
  99.     }
  100.  
  101.     MoveTo(20, 90 + ((numFiles + 1) * 20));
  102.     if (allOkay)
  103.         DrawString("\pWas able to open all files in FSSpec array.");
  104.     else {
  105.         DrawString("\pAargh! Had problems opening a file! Error: ");
  106.         NumToString(fsErr, tempStr);
  107.         DrawString(tempStr);
  108.     }
  109.     
  110.     while (!Button()) {}
  111. } // END main
  112.  
  113. void SetupApp() {
  114.     InitGraf(&thePort);
  115.     InitFonts();
  116.     FlushEvents(everyEvent,0);
  117.     InitWindows();
  118.     InitMenus();
  119.     TEInit();
  120.     InitDialogs(0L);
  121.     InitCursor();
  122.  
  123.     Rect tRect = screenBits.bounds;
  124.     InsetRect(&tRect, 80, 80);
  125.     bkgndWind = NewWindow(nil, &tRect, nil, true, plainDBox, (WindowPtr)-1, false, 0);
  126.     SetPort(bkgndWind);
  127.     
  128.     TextFont(geneva);
  129.     TextSize(9);
  130.     ForeColor(blueColor);
  131. } // END SetupApp
  132.  
  133.  
  134. // END File Scanner.c++